<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      finished-text=""
      @load="onLoad"
    >
    <purchase-status :purchaseList="purchaseList" :agreeShow="agreeShow"></purchase-status>
    </van-list>
    <no-data :noData='noData' v-if="purchaseList.length == 0 && finished"></no-data>
  </div>
</template>
<script>
import purchaseStatus from '../component/purchaseStatus'
import { myPurchaseList } from '@/api/dz/dzCcbd'
import noData from '@/components/noData/index'
import Vue from 'vue'
import { List } from 'vant'
Vue.use(List)
export default {
  components: {
    purchaseStatus,
    noData
  },
  data() {
    return {
      noData: {
        img: 'https://img.wifenxiao.com/h5-wfx/images/no_order.png',
        text: '暂无进货单',
        marginTop: '0px'
      },
      purchase_type: '',
      p: 1,
      loading: false,
      error: false,
      finished: true,
      purchaseList: [],
      agreeShow: false
    }
  },
  created() {
    if (this.$route.query.type) {
      this.purchase_type = this.$route.query.type
      if (this.purchase_type == 2) {
        document.title = '下级进货单'
        this.noData.text = '暂无下级进货单'
        this.agreeShow = true
      } else if (this.purchase_type == 3) {
        document.title = '我的升级进货单'
        this.noData.text = '暂无升级进货单'
        this.agreeShow = false
      } else if (this.purchase_type == 4) {
        document.title = '下级升级进货单'
        this.noData.text = '暂无下级升级进货单'
        this.agreeShow = true
      }
    } else {
      this.purchase_type = 1
      document.title = '我的进货单'
      this.noData.text = '暂无进货单'
      this.agreeShow = false
    }
    this.onLoad()
  },
  methods: {
    onLoad() {
      this.loading = true
      this.finished = false
      myPurchaseList({
        p: this.p,
        purchase_type: this.purchase_type
      }).then(res => {
        this.loading = false
        if (res.status == 1) {
          if (res.data.list.length > 0) {
            this.purchaseList.push(...res.data.list)
            this.purchaseList.forEach(item => {
              item.address = {
                address: '余杭街道马鞍山雅苑',
                address_id: '23235',
                area: '余杭区',
                area_id: '330110',
                card_img: null,
                city: '杭州市',
                city_id: '330100',
                create_time: '1640769440',
                delivery_status: 1,
                email: null,
                id_card: null,
                id_cert_name: '',
                is_default_address: '1',
                last_time: '1640769440',
                latitude: '30.265900',
                longitude: '119.956848',
                mobile: '15867247160',
                name: '吴江海',
                phone: '',
                province: '浙江省',
                province_id: '330000',
                town: '',
                town_id: null,
                use_num: '1',
                zip: '311100'
              }
            })
            this.p++
            this.finished = false
          } else {
            this.finished = true
          }
        } else {
          this.finished = true
          this.error = true
          this.$Error(res.msg)
        }
      })
        .catch(() => {
          this.error = true
        })
    }
  }
}
</script>